<template>
  <a-card>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row
          type="flex"
          justify="space-between"
        >
          <a-col
            :md="20"
            :sm="12"
          >
            <a-row :gutter="12">

              <a-col
                :md="8"
                :sm="8"
              >
                <a-form-item label="企业名称：">
                  <a-input
                    v-model="queryParam.enterpriseName"
                    placeholder="请输入企业名称"
                  />
                </a-form-item>
              </a-col>
              <a-col
                :md="8"
                :sm="4"
              >
                <a-form-item label="状态：">
                  <a-select
                    defaultValue=""
                    @change="change"
                  >
                    <a-select-option value="">全部</a-select-option>
                    <a-select-option value="0">待处理</a-select-option>
                    <a-select-option value="1">已寄出</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col
                :md="4"
                :sm="8"
              >
                <span class="table-page-search-submitButtons">
                  <a-button
                    type="primary"
                    icon="search"
                    @click="handleSearch"
                  >查询</a-button>
                </span>
              </a-col>
            </a-row>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(record, index)=> index"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
    >
      <span
        slot="action"
        slot-scope="text, record"
      >
        <template>
          <a
            @click="openEdit(record)"
            v-if="record.mailState===1"
            disabled>邮寄</a>
          <a
            @click="openEdit(record)"
            v-else>邮寄</a>
          <a-divider type="vertical" />
          <a @click="goDetail(record)">详情</a>
          <a-divider type="vertical" />
        </template>
      </span>
    </s-table>
    <more-bill ref="moreBill" />
    <a-modal
      title="邮寄"
      width="45%"
      :confirmLoading="confirmLoading"
      :visible="visible"
      @ok="handleSubmit"
      @cancel="handleCancel"
      destroyOnClose
    >
      <a-spin :spinning="confirmLoading">
        <a-form :form="form">
          <a-form-item
            label="物流公司"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <a-input
              placeholder="请输入物流公司名"
              v-decorator="['logisticsNameString', {
                rules: [{required: true, message: '请输入物流公司名'}]}]" />
          </a-form-item>
          <a-form-item
            label="快递单号"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <a-input
              placeholder="请输入快递单号"
              v-decorator="['trackingNumber', {
                rules: [{required: true, message: '请输入快递单号'}]}]" />
          </a-form-item>

        </a-form>
      </a-spin>
    </a-modal>
  </a-card>
</template>


<script>
import moreBill from './moreBill'
import { STable, Ellipsis } from '@/components'
import { queryInvoiceRecord , updateInvoiceRecord} from '@/api/manage'
import { formatTime, formatMoney } from '@/filter/index.js'

export default {
  name: 'Records',
  components: {
    STable,
    Ellipsis,
    moreBill
  },
  data() {
    return {
      //   queryCondition: {

      //   },
      edit:{},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      form: this.$form.createForm(this),
      confirmLoading: false,
      visible:false,
      ordersList: [],
      totalElements: 0,
      // 查询参数
      queryParam: {
        enterpriseName: '',
        mailState: '',
        pageNumber: 1,
        pageSize: 10
      },
      condition:{
        enterpriseName: '',
        status: '',
        pageNumber: 1,
        pageSize: 10,
        direction: 'DESC'
      },

      // 表头
      columns: [

        {
          title: '用户ID',
          dataIndex: 'userId',
        },
        {
          title: '用户名',
          dataIndex: 'userName',

          // sorter: true
          // needTotal: true,
          // customRender: (text) => text + ' 次'
        },
        {
          title: '企业名称',
          dataIndex: 'enterpriseName',

          //   scopedSlots: { customRender: 'membersAction' }
        },
        {
          title: '发票金额',
          dataIndex: 'invoiceAmount',
          customRender: (invoiceAmount) => '￥' + formatMoney(invoiceAmount)
          //   scopedSlots: { customRender: 'status' }
        },
        {
          title: '发票申请时间',
          dataIndex: 'applyTime',
          customRender: (applyTime) => formatTime(applyTime)
        },
        {
          title: '状态',
          dataIndex: 'mailState',
          customRender: (mailState) => {
            switch (mailState) {
              case 0:
                return '待处理'
              case 1:
                return '已寄出'
              default:
                return mailState
            }
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          // width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],

      // 加载数据方法 必须为 Promise 对象
      loadData: () => {
        return queryInvoiceRecord(Object.assign(this.condition)).then(res => {
          console.log(res)
          return res.resData
        })
      },
    }
  },
  mounted() {

  },
  methods: {
    fetchRecordList() {
        this.condition.enterpriseName = this.queryParam.enterpriseName,
        this.condition.status = this.queryParam.mailState,
        this.condition.pageNumber = this.queryParam.pageNumber,
        this.condition.pageSize = this.queryParam.pageSize,
        this.condition.direction = 'DESC'
      queryInvoiceRecord(this.condition)
        .then(res => {
          this.ordersList = res.resData.content
          this.totalElements = res.resData.totalElements
          console.log(res)
          return res.resData
        })
        .catch((err) => {
          console.log(err)
          // this.$notify.error({
          //   title: '获取发票地址失败',
          //   message: '系统异常，请联系管理员解决'
          // })
        })
    },
    handleSearch() {
      this.fetchRecordList()
      this.$refs.table.refresh()
    },
    // onChange(dateString) {
    //
    //   console.log(dateString)
    //   this.queryParam.enterpriseName = dateString
    // },
    change(date) {
      console.log(date)
      this.queryParam.mailState = date
    },

    openEdit(record){
      console.log(record)
      this.edit=record
      this.visible=true
    },

    goDetail(record){
      this.$refs.moreBill.show(record)
    },

    handleSubmit(){
      this.form.validateFields((err, value) => {
        console.log(value)
        if(!err){
        const Type = {
          id: this.edit.id,
          logisticsNameString: value.logisticsNameString,
          trackingNumber: value.trackingNumber,
        }
        // this.type=Type
        updateInvoiceRecord(Type)
        .then(res => {
          console.log(res)
          this.handleCancel()
          this.$refs.table.refresh()
        })
        }

      })
    },
    handleCancel(){
      this.visible=false
    }
  }
}
</script>
